﻿using eProcurement.BLL;
using eProcurement.controls;
using eProcurement.DAL;
using eProcurement.libs;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eProcurement.members
{
    public partial class OpenBids : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                Manager manager = new Manager();
                List<Project> projects = new List<Project>();

                switch ( Request[ "type" ] )
                {
                    case "state":
                        if ( Request[ "action" ] == "open" )
                        {
                            projects = manager.GetProjects( ProjectState.OPEN );
                            HydrateTable( projects );
                        }
                        break;
                    case "category":
                        projects = manager.GetProjectsByCategory( Request[ "action" ] );
                        HydrateTable( projects );
                        lblTitle.InnerText = "Open Bids - " + Request[ "action" ];
                        break;
                    case "company":
                        Purchaser purchaser = (Purchaser)manager.GetAppUser( Guid.Parse( Request[ "action" ].ToString() ) );
                        projects = purchaser.OpenProjects;
                        HydrateTable( projects );
                        lblsubtitle.InnerText = "The following are all open bids for " + purchaser.CompanyName;
                        break;
                    default:
                        break;
                }
            }
        }

        private void HydrateTable( List<Project> projects )
        {
            foreach ( Project project in projects )
            {
                ProjectItemControl control = (ProjectItemControl)LoadControl( "../controls/ProjectItemControl.ascx" );
                control.Setup( project );
                container.Controls.Add( control );
            }
        }
    }
}